//
//  CZNewFeatureViewController.swift
//  CY--weiBo
//
//  Created by mac on 15/11/28.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class CZNewFeatureViewController: UICollectionViewController {
    
    //重用标识
    private let ReuseIdentifier = "ReuseIdentifier"
    
    ///流水布局
    private var flowlayout = UICollectionViewFlowLayout()
    
    private let imageCount = 4
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //实现空参数的构造函数
    init() {
    
        //调用需要流水布局的构造函数,自己来提供流水布局
        super.init(collectionViewLayout: flowlayout)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
        prepareCollectionView()
        
    }
    
    private func prepareCollectionView() {
        //collectionView注册cell
        collectionView?.registerClass(CZNewFeatureCell.self, forCellWithReuseIdentifier: ReuseIdentifier)
        //设置布局
        //item大小
        flowlayout.itemSize = view.bounds.size
        
        //滚动方向
        flowlayout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        //设置间距为0
        flowlayout.minimumInteritemSpacing = 0
        flowlayout.minimumLineSpacing = 0
        
        //分页
        collectionView?.pagingEnabled = true
        
        //取消弹簧效果
        
        collectionView?.bounces = false
        
    }
    
    //CollectionView要显示数据需要实现数据源方法
    ///CollectionView要显示cell的数量
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return imageCount
    }
    /// 每个cell要显示的内容
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
       let cell = collectionView.dequeueReusableCellWithReuseIdentifier(ReuseIdentifier, forIndexPath: indexPath) as! CZNewFeatureCell
        
        cell.index = indexPath.item
        
        return cell
    }
    
    //停下来,判断是最后一个Cell的时候才做动画
    //一个cell,conectionView停下来,cell不可见的时候
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        
        //获取正在显示的cell的indexpath
        let visibleIndexPath = collectionView.indexPathsForVisibleItems().last!
        //在collectionView停下来,我们自己来判断是否是最后一个cell
        if visibleIndexPath.item == imageCount - 1 {
        
            //显示的时最后一个cell
            //根据indexPath获取cell
            let cell = collectionView.cellForItemAtIndexPath(visibleIndexPath)
            as! CZNewFeatureCell
            cell.startButtonAnimation()
        }
        
    }

}
    //自定义cell
    class CZNewFeatureCell: UICollectionViewCell {
        
        //MARK: - 属性
        
        var index: Int = 0 {
        
            didSet {
                //根据是第几个cell显示对应的图片
             bkgImageView.image = UIImage(named: "new_feature_\(index+1)")
                
                startButton.hidden = true
            }
        
        }
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
        
        override init(frame: CGRect) {
            super.init(frame: frame)
            
            prepareUI()
            
        }
    
    //MARK: - 按钮的点击事件
    func buttonClick() {
    
        //切换控制器
        AppDelegate.switchRootViewController(true)
    }
    
    func startButtonAnimation() {
        //显示按钮
        startButton.hidden = false
        
        //设置X/Y缩放都为0
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        
        UIView.animateWithDuration(1, delay: 0.1, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            //设置X/Y缩放都为1,清空到原始状态(X/Y缩放都为1)
            self.startButton.transform = CGAffineTransformIdentity
            
            }) { (_) -> Void in
                print("动画完成")
        }
    }
        

    func prepareUI() {
    
        //添加子控件
        contentView.addSubview(bkgImageView)
        contentView.addSubview(startButton)
        
        //添加约束
        bkgImageView.ff_Fill(contentView)
        
        startButton.ff_AlignInner(type: ff_AlignType.BottomCenter, referView: contentView, size: nil, offset: CGPoint(x: 0, y: -160))
    }
    
    //MARK: - 懒加载
    private lazy var bkgImageView: UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
    //开始按钮
    private lazy var startButton: UIButton = {
    
        let button = UIButton()
        
        //设置背景
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        
        //设置文字
        button.setTitle("开始体验", forState: UIControlState.Normal)
        
        //添加点击事件
        button.addTarget(self, action: "buttonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        
        return button
    
    }()
}
